/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.wsgi.container;

import java.util.HashMap;

import cc.aileron.wsgi.activity.WsgiController;
import cc.aileron.wsgi.container.uri.WsgiUriContainer;
import cc.aileron.wsgi.container.uri.WsgiUriContainerParameter;
import cc.aileron.wsgi.container.uri.WsgiUriContainerResult;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * 実装
 * 
 * @author Aileron
 */
@Singleton
public class WsgiContainerImpl implements WsgiContainer
{
    @Override
    public WsgiDto get(final FindCondition c)
    {
        final WsgiUriContainerResult result = container.get(c.uri,
                c.category,
                c.requestParameter.getKeys());
        if (result == null)
        {
            return WsgiDto.nullObject;
        }

        final HashMap<String, Object> parameters = new HashMap<String, Object>();
        for (final String key : c.requestParameter.getKeys())
        {
            parameters.put(key,
                    c.requestParameter.get(key));
        }
        parameters.putAll(result.uriParameters);
        return new WsgiDto(result.isThrough, parameters, map.get(result.id));
    }

    @Override
    public void put(final RegistryCondition c,
            final WsgiController controller)
    {
        final WsgiUriContainerParameter p = new WsgiUriContainerParameter();
        p.isThrough = c.isThrough;
        p.method = c.category;
        p.overrideKey = c.overrideKey;
        p.uri = c.uri;
        container.put(p);
        map.put(p.id,
                controller);
    }

    /**
     * constractor injection
     * 
     * @param container
     */
    @Inject
    public WsgiContainerImpl(final WsgiUriContainer container)
    {
        this.container = container;
    }

    /**
     * map
     */
    private final HashMap<Integer, WsgiController> map = new HashMap<Integer, WsgiController>();

    /**
     * container
     */
    private final WsgiUriContainer container;
}
